<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #555;
        }
        input[type="text"],
        input[type="password"],
        input[type="email"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .error-message {
            margin-top: 10px;
            font-weight: bold;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
    <h1>用户注册</h1>
    <form action="/user/register" method="post">
        <div>
            <label for="loginname">登录名:</label>
            <input type="text" id="loginname" name="loginname" required>
            <div id="loginname-validation" class="error-message"></div>
        </div>
        <div>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div>
            <label for="email">邮箱:</label>
            <input type="email" id="email" name="email" required>
        </div>
        <div class="error-message" style="color:red;">
            <span th:if="${error != null}" th:text="${error}"></span>
        </div>
        <button type="submit">注册</button>
    </form>
</div>

<script>
    $(document).ready(function() {
        $('#loginname').on('input', function() {
            var loginname = $(this).val();
            if (loginname.length > 0) {
                $.ajax({
                    url: '/user/checkRegistername',
                    type: 'GET',
                    data: { loginname: loginname },
                    success: function(response) {
                        if (response.exists) {
                            $('#loginname-validation').html('<p style="color: red;">登录名已存在</p>');
                        } else {
                            $('#loginname-validation').html('<p style="color: green;">登录名可用</p>');
                        }
                    },
                    error: function(xhr, status, error) {
                        $('#loginname-validation').html('<p>实时验证失效</p>');
                    }
                });
            } else {
                $('#loginname-validation').empty();
            }
        });
    });
</script>
</body>
</html>